﻿#-------------------------------------------------------------------------------
# Name:        dbCtrl.py
# Purpose:
#
# Author:      seewind
#
# Created:     20-02-2008
# Copyright:   (c) seewind 2008
# Licence:     <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import db
import storm.locals as storm

_useunicode = False
def useunicode(str):
    if _useunicode:
        return unicode(str)
    else:
        return str
    
class DbCtrl:
    def __init__(self, store):
        """ 初始化 """
        
        self._store = store
        #连接数据库
        self._db = storm.create_database(
            'mysql://root:@localhost/webstore?unicode=%s' % (str(_useunicode)))
        self._st = storm.Store(self._db)
        
        #检查、创建表结构
        self.createtables()

    def createtables(self):
        """ 检查、创建表结构 """
        try:
            t1 = self._st.find(db.DBPeer)
            t1.count()
            del t1
        except:
            #创建表
            try:
                self._st.execute(db.DBPeer.sql_createtable())
                self._st.execute(db.DBPiece.sql_createtable())
                self._st.execute(db.DBUrl.sql_createtable())
            except:
                self._st.rollback()

        
    def commit(self):
        pass
    def rollback(self):
        pass
    def flush(self):
        self._st.flush()

    def getdbpeer(self, peerids):
        if isinstance(peerids, (list, tuple)):
            return [self._st.get(db.DBPeer, useunicode(peerid)) for peerid in peerids]
        else:
            return self._st.get(db.DBPeer, useunicode(peerids))

    def getdburl(self, urlhashs):
        if isinstance(urlhashs, (list, tuple)):
            return [self._st.get(db.DBUrl, useunicode(urlhash)) for urlhash in urlhashs]
        else:
            return self._st.get(db.DBUrl, useunicode(urlhashs))
    
    def getdbpiece(self, ids):
        if isinstance(ids, (list, tuple)):
            return [self._st.get(db.DBPiece, id) for id in ids]
        else:
            return self._st.get(db.DBPiece, ids)
    
    def newdbpeer(self, peerid):
        p1 = db.DBPeer()
        p1.peerid = useunicode(peerid)
        self._st.add(p1)
        return p1

    def newdburl(self, urlhash):
        p1 = db.DBUrl()
        p1.urlhash = useunicode(urlhash)
        self._st.add(p1)
        return p1
        
    def newdbpiece(self):
        p1 = db.DBPiece()
        self._st.add(p1)
        return p1

    def findpeers(self, urlhash, piecestart, pieceend):
        if pieceend < piecestart:
            piecestart, pieceend = pieceend, piecestart
            
        rs_peers = self._st.find(db.DBPiece, db.DBPiece.urlhash==useunicode(urlhash),
            (db.DBPiece.piecestart<=piecestart) & (db.DBPiece.pieceend>=piecestart)
            | (db.DBPiece.piecestart>=piecestart) & (db.DBPiece.piecestart<=pieceend)
            )
        
        return rs_peers.order_by(db.DBPiece.piecestart)#Desc(db.DBPiece.piecestart)


